Are the brackets (IF, and END IF) properly balanced?

Answer:

Yes.

  • Every bracket has a match.
  • Each IF matches an END IF (the right kind of match).
  • The "inside" IF matches the "inside" END IF.
ask if customer wants coffee
IF customer wants coffee THEN
  charge for coffee

ELSE
  ' decide between tea and milk
  ask if customer wants tea
  IF customer wants tea THEN
    charge customer for tea
  ELSE
    charge customer for milk
  END IF

END IF

END

Nested IFs

The IF that asks about tea and milk matches the first END IF below it. The IF-THEN-ELSE-END IF is complete by itself, even though it is part of something larger.


ask if customer wants coffee
IF customer wants coffee THEN
  charge for coffee

ELSE
  ' decide between tea and milk
  ask if customer wants tea
  IF  customer wants tea THEN
    charge customer for tea
  ELSE
    charge customer for milk
  END IF

END IF

END

This arrangement of control structures is called nested IFs. When a computer program needs to make a choice between more than two branches, nested IFs are often used. If there are many choices, there will be many IFs. This can get very involved.

QUESTION 5:

Write two QBasic statements for this part of the plan for the program:

  ask if customer wants coffee

(Hint: use a character variable called ANSWER$ ).